#include "ActionPro.h"

uint8_t rx_data;
uint8_t rx_data2;

AcRxTemp_t AcRxTemp = {
	.step = 0,
	.cnt  = 0,
};

AcRxTemp_t RaspiRxTemp = {
	.step = 0,
	.cnt  = 0,
};

////时间不够了，乱写
//float py_angle;


/********* 发送部分 ***********/
/**
 * @brief  通信发送底层函数
 * @param  void
 * @retval void
 */
void Comm_tx(UART_HandleTypeDef *huart, uint8_t fun, uint8_t *data, uint8_t datalen)
{
    uint8_t SendBuff[32];
    uint8_t len = 0;

    /* 帧头 */
    SendBuff[len++] = 0xFD;
    SendBuff[len++] = 0xDF;

    /* 总长度 先占位 */
    SendBuff[len++] = 0;

    /* 功能帧 */
    SendBuff[len++] = fun;

    /* 数据 */
    for (int max = (datalen + len), now = len; len < max; len++) {
        SendBuff[len] = data[len - now];
    }

    /* 获取实际长度 */
    SendBuff[2] = len;

    /* 校验位 */
    SendBuff[len] = 0;
    for (int i = 0; i < len; i++) {
        SendBuff[len] += SendBuff[i];
    }
		
		HAL_UART_Transmit(huart, SendBuff, len+1, 0xff);
}

/**
 * @brief  发送16位数据
 * @param  void
 * @retval void
 */
void Comm_tx_uint16(UART_HandleTypeDef *huart, uint8_t fun, uint16_t data)
{
    uint8_t txdata[2];
    txdata[0] = (uint8_t)(data >> 8);
    txdata[1] = (uint8_t)data;

    Comm_tx(huart, fun, txdata, 2);
}

/********* 接收部分 **********/
/**
 * @brief  校验位计算
 * @param  void
 * @retval void
 */
uint8_t rx_verify(uint8_t *data, uint8_t len)
{
	uint8_t verify = 0x00U;
	
	for (uint8_t i=0; i < len; i++) {
		verify += data[i];
	}	
	
	return verify;
}

/**
 * @brief  串口位置解析
 * @param  void
 * @retval void
 */
void Position_Analysis(uint8_t data[])
{
	if ( rx_verify(data, data[2]) == data[data[2]]) {	/* 校验通过 */
		
		if (data[3] == 0xf1) {	/* 数据帧 */
			key_buff[0] = data[4];
			key_buff[1] = data[5];
			key_buff[2] = data[6];
		} else if (data[3] == 0xe1) {
			
//			for (uint8_t i=0; i < 9; i++) {
//				opencvchessboard[i] = data[4+4+i];
//			}
//			for (uint8_t i=0; i < 9; i++) {
//				lookchessboard[i] = opencvchessboard[i];
//			}
			
			extern uint8_t xz_flag;
			extern uint8_t user_xz_flag;
			if (user_xz_flag == 1 && xz_flag == 1) {
				lookchessboard[8] = data[4+4+8];
				lookchessboard[7] = data[4+4+7];
				lookchessboard[6] = data[4+4+6];
				
				lookchessboard[5] = data[4+4+5];
				lookchessboard[4] = data[4+4+4];
				lookchessboard[3] = data[4+4+3];
				
				lookchessboard[2] = data[4+4+2];
				lookchessboard[1] = data[4+4+1];
				lookchessboard[0] = data[4+4+0];
			} else {
				lookchessboard[0] = data[4+4+8];
				lookchessboard[1] = data[4+4+7];
				lookchessboard[2] = data[4+4+6];
				
				lookchessboard[3] = data[4+4+5];
				lookchessboard[4] = data[4+4+4];
				lookchessboard[5] = data[4+4+3];
				
				lookchessboard[6] = data[4+4+2];
				lookchessboard[7] = data[4+4+1];
				lookchessboard[8] = data[4+4+0];
			}
//			opencvchessboard[0] = data[4+4+8];
//			opencvchessboard[1] = data[4+4+7];
//			opencvchessboard[2] = data[4+4+6];
//			
//			opencvchessboard[3] = data[4+4+5];
//			opencvchessboard[4] = data[4+4+4];
//			opencvchessboard[5] = data[4+4+3];
//			
//			opencvchessboard[6] = data[4+4+2];
//			opencvchessboard[7] = data[4+4+1];
//			opencvchessboard[8] = data[4+4+0];
			lookchessboard[0] = data[4+4+8];
			lookchessboard[1] = data[4+4+7];
			lookchessboard[2] = data[4+4+6];
			
			lookchessboard[3] = data[4+4+5];
			lookchessboard[4] = data[4+4+4];
			lookchessboard[5] = data[4+4+3];
			
			lookchessboard[6] = data[4+4+2];
			lookchessboard[7] = data[4+4+1];
			lookchessboard[8] = data[4+4+0];			

			fl_u8_t angle;
			angle.u8[0] = data[4];
			angle.u8[1] = data[5];
			angle.u8[2] = data[6];
			angle.u8[3] = data[7];
			
//			py_angle = angle.f;
			//angle.f = -angle.f;
			
			/* 限幅平均滤波 */
			pressureFilter(&angle.f, &angle_arror);
			
			static uint8_t i=0;
			if (i++ < 10) {
				angle_init = angle_init + angle.f * 0.1f;
			} else {
				i = 20;
			}
			//angle_arror = angle_arror - angle_init;
		} else {
			/* ... */
		}
	}
}

/**
 * @brief  通过串口接收命令
 * @param  void
 * @retval void
 */
void ActionRx_ForUart(AcRxTemp_t * AcRxTemp, uint8_t data)
{
	switch(AcRxTemp->step) {
		case 0:
			AcRxTemp->cnt = 0;
			AcRxTemp->buff[AcRxTemp->cnt++] = data;
			if (data == 0xFD) {
				AcRxTemp->step = 1;
			}
			break;
		case 1:
			AcRxTemp->buff[AcRxTemp->cnt++] = data;
			if (data == 0xDF) {
				AcRxTemp->step = 2;
			} else {
				AcRxTemp->step = 0;
			}
			break;
		case 2:
			AcRxTemp->buff[AcRxTemp->cnt++] = data;
		
			if (AcRxTemp->cnt - 1 == 2) {
				AcRxTemp->rxlen = data;
			}
			
			if (AcRxTemp->cnt > AcRxTemp->rxlen) {
				AcRxTemp->step = 3;
				AcRxTemp->cnt = 0;
			}
			
			break;
			
		default :
		{};
	}
	
	if (AcRxTemp->step == 3) {
		AcRxTemp->step = 0;
		Position_Analysis(AcRxTemp->buff);
	}
}

/**
 * @brief 串口中断处理
* @param  无
 * @retval 无
**/
void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart)
{
	if(huart == Action_useUart)
	{
		ActionRx_ForUart(&AcRxTemp, rx_data);
		HAL_UART_Receive_IT(Action_useUart, &rx_data,sizeof(rx_data));
	}
	
	if(huart == RaspberryPi_useUart)
	{
		ActionRx_ForUart(&RaspiRxTemp, rx_data2);
		HAL_UART_Receive_IT(RaspberryPi_useUart, &rx_data2,sizeof(rx_data2));
	}	
}

/**
 * @brief  定位系统初始化
 * @param  void
 * @retval void
 */
void Action_init(void)
{
//	Action_Reset();	/* 复位 */
	
	/* 开启接收 */
	HAL_UART_Receive_IT(Action_useUart, &rx_data,sizeof(rx_data));
	HAL_UART_Receive_IT(RaspberryPi_useUart, &rx_data2,sizeof(rx_data2));
}

